using UnityEngine;
using System.Collections;
using System.Reflection;

public class GUIDuctTape : MonoBehaviour {
	public bool debug = false;
	private bool displayAllRolls = false;
	
	private bool display = true;
	
	public Vector2 position		=	new Vector2(0,0);
	public Vector2 squareSize	=	new Vector2(250,250);
	
	public Texture2D ductTapeTexture1;
	public Texture2D ductTapeTexture2;
	public Texture2D ductTapeTexture3;
	public Texture2D ductTapeTexture4;
	public Texture2D ductTapeTexture5;
	public Texture2D ductTapePileTexture1;
	public Texture2D ductTapePileTexture2;
	public Texture2D ductTapePileTexture3;
	public Texture2D ductTapePileTexture4;
	
	
	private int ductTapeAmount = 0;

	public Vector2 ductTapeRollPosition1	=	new Vector2(0,0);
	public Vector2 ductTapeRollPosition2	=	new Vector2(0,0);
	public Vector2 ductTapeRollPosition3	=	new Vector2(0,0);
	public Vector2 ductTapeRollPosition4	=	new Vector2(0,0);
	private ArrayList ductTapeArray;
	
	public Vector2[] ductTapeRollPositions;
	
	public Texture2D[] ductTapeTextures;
	
	private Texture2D testTexture;
	
	void Awake(){
	// Check if textures are missing
		foreach (FieldInfo fi in this.GetType().GetFields()){
			if (fi.FieldType == typeof(Texture2D)){
				if (fi.GetValue(this).ToString() == "null"){
					Texture2D newTexture = new Texture2D(100,100);
					newTexture.Apply();
					fi.SetValue(this,newTexture);
					if (fi.Name != "testSquare")
						Debug.LogWarning("Texture " + fi.Name + " in " + this.GetType() + " in gameojbect " + this.name + " is missing.");
				}
			}
		}	
	}
	
	// Use this for initialization
	void Start () {
		testTexture = new Texture2D(1,1);
		testTexture.SetPixel(0,0,Color.red);
		testTexture.Apply();
		
		ductTapeTextures 		= new Texture2D[]{	ductTapeTexture1,
													ductTapeTexture2,
													ductTapeTexture3,
													ductTapeTexture4,
													ductTapeTexture5,
													ductTapePileTexture1,
													ductTapePileTexture2,
													ductTapePileTexture3,
													ductTapePileTexture4};
		
	}
	
	// Update is called once per frame
	void Update () {
		ductTapeRollPositions 	= new Vector2[]{ductTapeRollPosition1,ductTapeRollPosition2,ductTapeRollPosition3,ductTapeRollPosition4};
	}
	
	void OnGUI(){
		if (display){
		GUI.BeginGroup(new Rect(position.x,position.y,squareSize.x,squareSize.y));
		if (debug) {
			GUI.DrawTexture(new Rect(0,0,squareSize.x,squareSize.y),testTexture,ScaleMode.StretchToFill);
			
		}

		
		int loopNumber = ((ductTapeAmount-5)%4+1);
		loopNumber = (ductTapeAmount-loopNumber)/4;
		if (ductTapeAmount > 4)
		for (int i = 0; i < Mathf.Clamp(loopNumber,0,12); i++) {
			//Debug.Log("i = " + i.ToString() + "loopnumber = " + loopNumber.ToString());
			GUI.DrawTexture(new Rect(ductTapeRollPositions[i/4+1].x,ductTapeRollPositions[i/4+1].y,ductTapeTexture5.width,ductTapeTexture5.height),ductTapeTextures[5+i%4]);
			
		}
		
		
		if (ductTapeAmount > 4)
			GUI.DrawTexture(new Rect(ductTapeRollPosition1.x,ductTapeRollPosition1.y,ductTapeTexture5.width,ductTapeTexture5.height),ductTapeTextures[(ductTapeAmount-5)%4+1]);
		else
			GUI.DrawTexture(new Rect(ductTapeRollPosition1.x,ductTapeRollPosition1.y,ductTapeTexture5.width,ductTapeTexture5.height),ductTapeTextures[ductTapeAmount%5]);
		
		
		//GUI.
		GUI.EndGroup();
		
		if (debug){
			GUI.Label(new Rect(Screen.width/2-50,Screen.height-75,100,25),"Tape Amount: " + ductTapeAmount.ToString(),"box");
			if (GUI.Button(new Rect(Screen.width/2-50,Screen.height-100,100,25),"Tape +1","box"))
				ductTapeAmount += 1;
			if (GUI.Button(new Rect(Screen.width/2-50,Screen.height-125,100,25),"Tape -1","box") && ductTapeAmount > 0)
				ductTapeAmount -= 1;
		}
		}
	}	
	public void setDuctTapeAmount(int amount){
		ductTapeAmount = amount;	
	}
	
	public void setDisplay(bool Display){
		display = Display;	
	}
	
}
